package cn.ad.server.persistence.dao;

import cn.ad.server.domain.ad.AdSpace;
import cn.ad.server.domain.ad.AdSpaceAdxMap;
import cn.ad.server.persistence.typehandler.EnumStatusTypeHandler;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface AdSpaceDao {

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "ratio_id", property = "ratio.id"),
			@Result(column = "pub_site_id", property = "pubSite.id"),
			@Result(column = "format_id", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			/*@Result(column = "template_id", property = "adTemplate.id"),*/
			@Result(column = "template_id",property ="adTemplate",many = @Many(select = "cn.ad.server.persistence.dao.AdTemplateDao.selectByIdWithSpecs" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "volume_limit", property = "volumeLimit"),
			@Result(column = "buy_type", property = "buyType"),
			@Result(column = "charge_type", property = "chargeType"),
			@Result(column = "charge_price", property = "chargePrice"),
			@Result(column = "default_creative_id", property = "defaultCreative.id"),
			@Result(column = "default_creative_option", property = "defaultCreativeOption"),
			@Result(column = "id",property ="adSpaceAdxMaps",many = @Many(select = "findAdSpaceAdxMapByAdspaceId" ,fetchType = FetchType.DEFAULT)),
			})
	@Select("select * from ad_space")
	List<AdSpace> findAll();

	@Results(value = {
			@Result(column = "ad_space_id", property = "adSpaceId"),
			@Result(column = "adx_space_id", property = "adxSpaceId"),
			@Result(column = "channel_id", property = "channelId"),
			@Result(column = "adx_type", property = "adxType"),
			@Result(column = "app_id", property = "appId"),
			@Result(column = "package_name", property = "packageName"),
			@Result(column = "os", property = "os"),
			@Result(column = "width", property = "width"),
			@Result(column = "height", property = "height"),
			@Result(column = "spec_map", property = "specMap"),
			@Result(column = "status", property = "status"),
			@Result(column = "data_format", property = "dataFormat"),
			@Result(column = "request_ad_format", property = "requestAdFormat"),
			@Result(column = "priority", property = "priority")
	})
	@Select("select * from ad_space_adx_map where ad_space_id = #{id} and status = 1 order by priority asc")
	List<AdSpaceAdxMap> findAdSpaceAdxMapByAdspaceId(int id);

	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "ratio_id", property = "ratio.id"),
			@Result(column = "pub_site_id", property = "pubSite.id"),
			@Result(column = "format_id", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "volume_limit", property = "volumeLimit"),
			@Result(column = "buy_type", property = "buyType"),
			@Result(column = "charge_type", property = "chargeType"),
			@Result(column = "charge_price", property = "chargePrice"),
			@Result(column = "default_creative_id", property = "defaultCreative.id"),
			@Result(column = "default_creative_option", property = "defaultCreativeOption"),
			@Result(column = "id",property ="adSpaceAdxMaps",many = @Many(select = "findAdSpaceAdxMapByAdspaceId" ,fetchType = FetchType.DEFAULT)),
			})
	@Select("select * from ad_space where update_time >= #{value}")
	List<AdSpace> findAfterTime(String warmUpTime);
	
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "ratio_id", property = "ratio.id"),
			@Result(column = "pub_site_id", property = "pubSite.id"),
			@Result(column = "format_id", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "volume_limit", property = "volumeLimit"),
			@Result(column = "buy_type", property = "buyType"),
			@Result(column = "charge_type", property = "chargeType"),
			@Result(column = "charge_price", property = "chargePrice"),
			@Result(column = "default_creative_id", property = "defaultCreative.id"),
			@Result(column = "default_creative_option", property = "defaultCreativeOption"),
			@Result(column = "id",property ="adSpaceAdxMaps",many = @Many(select = "findAdSpaceAdxMapByAdspaceId" ,fetchType = FetchType.DEFAULT)),
	})
	@Select("select * from ad_space where status = 1")
	List<AdSpace> findValidAll();
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "ratio_id", property = "ratio.id"),
			@Result(column = "pub_site_id", property = "pubSite.id"),
			@Result(column = "format_id", property = "adFormat",typeHandler=EnumStatusTypeHandler.class),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "volume_limit", property = "volumeLimit"),
			@Result(column = "buy_type", property = "buyType"),
			@Result(column = "charge_type", property = "chargeType"),
			@Result(column = "charge_price", property = "chargePrice"),
			@Result(column = "default_creative_id", property = "defaultCreative.id"),
			@Result(column = "default_creative_option", property = "defaultCreativeOption"),
			@Result(column = "id",property ="adSpaceAdxMaps",many = @Many(select = "findAdSpaceAdxMapByAdspaceId" ,fetchType = FetchType.DEFAULT)),
	})
	@Select("select * from ad_space where id = #{id}")
	AdSpace findById(long id);

}
